Public Class COrderStatus
    Inherits BOBase

    Protected m_State As OrderStatusDS
    Protected m_DA As OrderStatuss
    Protected _OrderDate As Date

#Region "   Inherits Implementation"
    Sub New()
        m_State = New OrderStatusDS
        m_DA = New OrderStatuss
    End Sub

    Public Overrides ReadOnly Property DA() As DABase
        Get
            If m_DA Is Nothing Then
                m_DA = New OrderStatuss
            End If

            Return m_DA
        End Get
    End Property

    Public Overrides Property ObjectState() As DataSet
        Get
            Return m_State
        End Get
        Set(ByVal Value As DataSet)
            Try
                m_State = CType(Value, OrderStatusDS)
            Catch ex As InvalidCastException
                Throw New ApplicationException("Invalid ORDER_STATUS Dataset!", ex)
            End Try
        End Set
    End Property

    Public Overrides Sub Init()
        Me.m_State.Clear()
        Dim dr As OrderStatusDS.ORDER_STATUSRow = m_State.ORDER_STATUS.NewORDER_STATUSRow
        With dr
            .ORDER_ID_FK = Nothing
            .LAST_UPDATED = Now.Today
            .PERCENT_COMPLETED = Nothing
            .EST_TIME_DELIVERY = Nothing
            .PREV_EST_TIME_DELIVERY = Nothing
            .SetDEPARTURE_DATENull()
            .SetVESSELNull()
            .SetEST_TIME_ARRIVALNull()
            .SetPREV_EST_TIME_ARRIVALNull()
            .SetDOC_SENTNull()
            .SetDOC_SENT_DATENull()
            .SetPAY_RECEIVEDNull()
            .SetPAY_RECEIVED_DATENull()
            .SetINVOICE_NONull()
            .SetINVOICE_DATENull()
            .SetINVOICE_VALUENull()
            .SetCOSTNull()
            .SetCREATED_BYNull()
            .SetCREATED_DATENull()
            .SetUPDATED_BYNull()
        End With
        m_State.ORDER_STATUS.Rows.Add(dr)
    End Sub
#End Region

#Region "   Data Properties"
    Protected ReadOnly Property DataRow() As OrderStatusDS.ORDER_STATUSRow
        Get
            Return CType(Me.Row, OrderStatusDS.ORDER_STATUSRow)
        End Get
    End Property

    Public ReadOnly Property ID() As System.Int32
        Get
            Return Me.DataRow.OS_ID_PK
        End Get
    End Property

    Public Property OrderID() As System.Int32
        Get
            Return Me.DataRow.ORDER_ID_FK
        End Get
        Set(ByVal Value As System.Int32)
            Me.DataRow.ORDER_ID_FK = Value
        End Set
    End Property

    Public Property LastUpdated() As System.DateTime
        Get
            If DataRow.IsLAST_UPDATEDNull Then
                Return Nothing
            End If
            Return Me.DataRow.LAST_UPDATED
        End Get
        Set(ByVal Value As System.DateTime)
            If Value = Nothing Then
                Me.DataRow.SetLAST_UPDATEDNull()
            Else
                Me.DataRow.LAST_UPDATED = Value
            End If
        End Set
    End Property

    Public Property PercentCompleted() As System.Decimal
        Get
            If Me.DataRow.IsPERCENT_COMPLETEDNull Then
                Return Nothing
            Else
                Return Me.DataRow.PERCENT_COMPLETED
            End If
        End Get
        Set(ByVal Value As System.Decimal)
            Me.DataRow.PERCENT_COMPLETED = Value
        End Set
    End Property

    Public Property PercentCompletedToNull() As System.Boolean
        Get
            Return Me.DataRow.IsPERCENT_COMPLETEDNull()
        End Get
        Set(ByVal Value As System.Boolean)
            Me.DataRow.SetPERCENT_COMPLETEDNull()
        End Set
    End Property

    Public Property EstTimeDelivery() As System.DateTime
        Get
            If DataRow.IsEST_TIME_DELIVERYNull Then
                Return Nothing
            End If
            Return Me.DataRow.EST_TIME_DELIVERY
        End Get
        Set(ByVal Value As System.DateTime)
            If Value = Nothing Then
                Me.DataRow.SetEST_TIME_DELIVERYNull()
            Else
                Me.DataRow.EST_TIME_DELIVERY = Value
            End If
        End Set
    End Property

    Public Property PrevEstTimeDelivery() As System.DateTime
        Get
            If DataRow.IsPREV_EST_TIME_DELIVERYNull Then
                Return Nothing
            End If
            Return Me.DataRow.PREV_EST_TIME_DELIVERY
        End Get
        Set(ByVal Value As System.DateTime)
            If Value = Nothing Then
                Me.DataRow.SetPREV_EST_TIME_DELIVERYNull()
            Else
                Me.DataRow.PREV_EST_TIME_DELIVERY = Value
            End If
        End Set
    End Property

    Public Property DepartureDate() As System.DateTime
        Get
            If DataRow.IsDEPARTURE_DATENull Then
                Return Nothing
            End If
            Return Me.DataRow.DEPARTURE_DATE
        End Get
        Set(ByVal Value As System.DateTime)
            If Value = Nothing Then
                Me.DataRow.SetDEPARTURE_DATENull()
            Else
                Me.DataRow.DEPARTURE_DATE = Value
            End If
        End Set
    End Property

    Public Property Vessel() As System.String
        Get
            If DataRow.IsVESSELNull Then
                Return Nothing
            End If
            Return Me.DataRow.VESSEL
        End Get
        Set(ByVal Value As System.String)
            If Value = Nothing Then
                Me.DataRow.SetVESSELNull()
            Else
                Me.DataRow.VESSEL = Value
            End If
        End Set
    End Property

    Public Property EstTimeArrival() As System.DateTime
        Get
            If DataRow.IsEST_TIME_ARRIVALNull Then
                Return Nothing
            End If
            Return Me.DataRow.EST_TIME_ARRIVAL
        End Get
        Set(ByVal Value As System.DateTime)
            If Value = Nothing Then
                Me.DataRow.SetEST_TIME_ARRIVALNull()
            Else
                Me.DataRow.EST_TIME_ARRIVAL = Value
            End If
        End Set
    End Property

    Public Property PrevEstTimeArrival() As System.DateTime
        Get
            If DataRow.IsPREV_EST_TIME_ARRIVALNull Then
                Return Nothing
            End If
            Return Me.DataRow.PREV_EST_TIME_ARRIVAL
        End Get
        Set(ByVal Value As System.DateTime)
            If Value = Nothing Then
                Me.DataRow.SetPREV_EST_TIME_ARRIVALNull()
            Else
                Me.DataRow.PREV_EST_TIME_ARRIVAL = Value
            End If
        End Set
    End Property

    Public Property DocSent() As System.Boolean
        Get
            If DataRow.IsDOC_SENTNull Then
                Return Nothing
            End If
            Return Me.DataRow.DOC_SENT
        End Get
        Set(ByVal Value As System.Boolean)
            If Value = Nothing Then
                Me.DataRow.SetDOC_SENTNull()
            Else
                Me.DataRow.DOC_SENT = Value
            End If

        End Set
    End Property

    Public Property DocSentDate() As System.DateTime
        Get
            If Me.DataRow.IsDOC_SENT_DATENull Then
                Return Nothing
            Else
                Return Me.DataRow.DOC_SENT_DATE
            End If
        End Get
        Set(ByVal Value As System.DateTime)
            If Value = Nothing Then
                Me.DataRow.SetDOC_SENT_DATENull()
            Else
                Me.DataRow.DOC_SENT_DATE = Value
            End If
        End Set
    End Property

    Public Property PayReceived() As System.Boolean
        Get
            If DataRow.IsPAY_RECEIVEDNull Then
                Return Nothing
            End If
            Return Me.DataRow.PAY_RECEIVED
        End Get
        Set(ByVal Value As System.Boolean)
            If Value = Nothing Then
                Me.DataRow.SetPAY_RECEIVEDNull()
            Else
                Me.DataRow.PAY_RECEIVED = Value
            End If
        End Set
    End Property

    Public Property PayReceivedDate() As System.DateTime
        Get
            If Me.DataRow.IsPAY_RECEIVED_DATENull Then
                Return Nothing
            Else
                Return Me.DataRow.PAY_RECEIVED_DATE
            End If
        End Get
        Set(ByVal Value As System.DateTime)
            If Value = Nothing Then
                Me.DataRow.SetPAY_RECEIVED_DATENull()
            Else
                Me.DataRow.PAY_RECEIVED_DATE = Value
            End If
        End Set
    End Property

    Public Property InvoiceNo() As System.String
        Get
            If Me.DataRow.IsINVOICE_NONull Then
                Return Nothing
            End If
            Return Me.DataRow.INVOICE_NO
        End Get
        Set(ByVal Value As System.String)
            If Value = Nothing Then
                Me.DataRow.SetINVOICE_NONull()
            Else
                Me.DataRow.INVOICE_NO = Value
            End If

        End Set
    End Property

    Public Property InvoiceDate() As System.DateTime
        Get
            If Me.DataRow.IsINVOICE_DATENull Then
                Return Nothing
            End If
            Return Me.DataRow.INVOICE_DATE
        End Get
        Set(ByVal Value As System.DateTime)
            If Value = Nothing Then
                Me.DataRow.SetINVOICE_DATENull()
            Else
                Me.DataRow.INVOICE_DATE = Value
            End If

        End Set
    End Property

    Public Property InvoiceValue() As System.Decimal
        Get
            If Me.DataRow.IsINVOICE_VALUENull Then
                Return Nothing
            Else
                Return Me.DataRow.INVOICE_VALUE
            End If
        End Get
        Set(ByVal Value As System.Decimal)
            Me.DataRow.INVOICE_VALUE = Value
        End Set
    End Property

    Public Property InvoiceValueToNull() As System.Boolean
        Get
            Return Me.DataRow.IsINVOICE_VALUENull()
        End Get
        Set(ByVal Value As System.Boolean)
            Me.DataRow.SetINVOICE_VALUENull()
        End Set
    End Property

    Public Property Cost() As System.Decimal
        Get
            If Me.DataRow.IsCOSTNull Then
                Return Nothing
            Else
                Return Me.DataRow.COST
            End If
        End Get
        Set(ByVal Value As System.Decimal)
            Me.DataRow.COST = Value
        End Set
    End Property

    Public Property CostToNull() As System.Boolean
        Get
            Return Me.DataRow.IsCOSTNull()
        End Get
        Set(ByVal Value As System.Boolean)
            Me.DataRow.SetCOSTNull()
        End Set
    End Property

    Public Property CreatedBy() As System.Int32
        Get
            If Me.DataRow.IsCREATED_BYNull Then
                Return Nothing
            Else
                Return Me.DataRow.CREATED_BY
            End If
        End Get
        Set(ByVal Value As System.Int32)
            If Value = Nothing Then
                Me.DataRow.SetCREATED_BYNull()
            Else
                Me.DataRow.CREATED_BY = Value
            End If
        End Set
    End Property

    Public Property CreatedDate() As System.DateTime
        Get
            If Me.DataRow.IsCREATED_DATENull Then
                Return Nothing
            Else
                Return Me.DataRow.CREATED_DATE
            End If
        End Get
        Set(ByVal Value As System.DateTime)
            If Value = Nothing Then
                Me.DataRow.SetCREATED_DATENull()
            Else
                Me.DataRow.CREATED_DATE = Value
            End If
        End Set
    End Property

    Public Property UpdatedBy() As System.Int32
        Get
            If Me.DataRow.IsUPDATED_BYNull Then
                Return Nothing
            Else
                Return Me.DataRow.UPDATED_BY
            End If
        End Get
        Set(ByVal Value As System.Int32)
            If Value = Nothing Then
                Me.DataRow.SetUPDATED_BYNull()
            Else
                Me.DataRow.UPDATED_BY = Value
            End If
        End Set
    End Property

    Public ReadOnly Property Week() As Integer
        Get
            Return Common.Week(Me.LastUpdated)
        End Get
    End Property

    Public ReadOnly Property Year() As Integer
        Get
            Return Common.Year(Me.LastUpdated)
        End Get
    End Property

    ' this property is set outside this class
    ' and not belong to OrderStatus table
    Public Property OrderDate() As Date
        Get
            Return Me._OrderDate
        End Get
        Set(ByVal Value As Date)
            Me._OrderDate = Value
        End Set
    End Property
#End Region

    Public Overloads Sub Load(ByVal orderID As Object, ByVal week As Integer, ByVal year As Integer)
        Dim keys(2) As Object
        keys(0) = orderID
        keys(1) = week
        keys(2) = year

        MyBase.Load(keys)
    End Sub

    Public Overloads Sub LoadLastestOrderStatus(ByVal orderID As Object)
        Me.m_DA.GetLastestOrderStatus(orderID, m_State)
    End Sub

    Public Function LastWeek() As COrderStatus
        Dim objOS As New COrderStatus
        Dim week As Integer = Me.Week
        Dim year As Integer = Me.Year

        If week - 1 <= 0 Then
            year -= 1
            week = 52
        Else
            week -= 1
        End If

        Try
            objOS.Load(Me.OrderID, week, year)
        Catch ex As Exception
            Throw New ApplicationException("Failed to load last week data", ex)
        End Try

        Return objOS
    End Function

    Public Function GetLastestOrderStatusList(ByVal customerID As Integer, ByVal supplierID As Integer) As DataSet
        Return m_DA.GetLastestOrderStatusList(customerID, supplierID)
    End Function

    Public Shared Function GetOrderStatusWeeklyList(ByVal customerID As Integer) As DataSet
        Dim ds As DataSet
        ds = DataHelper.ExecuteDataSet2("GetOrderStatusWeeklyList", customerID)
        Return ds
    End Function

    Public Shared Function GetOrderStatusReport(ByVal supplierID As Integer, ByVal ETAportID As Integer, ByVal week As Integer, ByVal year As Integer, ByVal customerID As Integer) As DataTable
        Dim ds As DataSet
        ds = DataHelper.ExecuteDataSet2("OrderStatusReportByWeekYear", supplierID, ETAportID, week, year, customerID)
        Return ds.Tables(0)
    End Function

    Public Shared Function FillinMissingOrderStatus(ByVal orderID As Integer, ByVal nowDate As Date) As Integer
        Return DataHelper.ExecuteNonQuery("FillinMissingOrderStatus", orderID, nowDate)
    End Function

    Public Shared Function ValidateUniqueInvoiceNo(ByVal orderStatusID As Integer, ByVal invoiceNo As String, ByVal osIDs As ArrayList) As Boolean
        Dim query As String
        query = String.Format(" SELECT OS_ID_PK FROM ORDER_STATUS WHERE INVOICE_NO=@INVOICE_NO AND OS_ID_PK<>{0} ", orderStatusID)

        Dim i As Integer
        For i = 0 To osIDs.Count - 1
            query = query + String.Format(" AND OS_ID_PK<>{0} ", osIDs(i))
        Next


        Dim sqlCmd As New SqlCommand(query)
        sqlCmd.Parameters.Add("@INVOICE_NO", invoiceNo)

        Dim obj As Object
        obj = DataHelper.ExecuteScalar(sqlCmd)

        If (obj <> Nothing) Then
            Return False
        Else
            Return True
        End If
    End Function

End Class
